/*************************************************************************
C *** This program is proprietary to ELSAG BAILEY and may only be used ***
C *** as authorized  in a license agreement controlling such use.      ***
C*************************************************************************
C
C  10  Name
C
C  20  Purpose
C
C  30  Description
C       Author        Date         Version    Reason
C       ---------    -----------   -------   ---------------------------------
C
C  40  Calling Sequence/Execution Criteria
C
C  50  Common Usage
C  60  Input/Output
C  70  Method
C
C***********************************************************************/
#define ALONE_TWN_MOV_EXEC
#include "Twn_Exec_Mov.h"


/********************************************************************************/
/*																				*/
/*	Function:		Exec_Seq													*/
/*	Description:	Take a macro op and split in micro-op usinf F_OSEQ data.	*/
/*					Movement and TV are managed by other functions.				*/
/*	Input:			Code		-	Macro operation code.						*/
/*					ObjectIdx	-	Idx of the object to move.					*/
/*					ObjtIdx		-	Idx of the object type to move.				*/
/*					ZoneIdx		-	Idx of the zone to move object into.		*/
/*					VCoo_X		-	Virtual X coor. where perform macro-op.		*/
/*					VCoo_Y		-	Virtual Y coor. where perform macro-op.		*/
/*					ObjtCell	-	Situation in the cell.						*/
/*					ObjtMast	-	Situation on Main mast.						*/
/*	Return:			PH_NORMAL	-	Success										*/
/*																				*/
/********************************************************************************/
PHASTAT Exec_Seq (	int Code, GENIDX ObjectIdx, GENIDX ObjtIdx, GENIDX ZoneIdx, short int VCoo_X, 
					short int VCoo_Y, OBJTCELL *ObjtCell, OBJTMM *ObjtMast){
	RETSTAT			stat;
	RETCOND			retstat;
	GENIDX			SeqIdx;
	FILEIDX			file;
	MIC_CMDNAME		MicroCmdName;
	GENIDX			MicroCmdIdx;
	GENIDX			micrIdx;
	GENIDX			FAGRLIdx;
	t_ope_seq		seq;
	int				Current_Phase;
	int				next_move_a;
	ZONENAME		ZoneName;
	micro_cmd_exe	micro_exe;
	int				i;
	int				objtGenCode;
	int				cellType;
	int				markReq;

	/* GET SEQUENCE IDX USING: CODE, OBJECT TYPE IDX, ZONE IDX */
	/* Get zone name in case of Home Main Mast use a special GetSeq */
	stat = GetZoneName ( ZoneIdx, ZoneName );
	if(stat !=RT_NORMAL){
		_stprintf_s(EXM_LogMsg, _T("Error: Exec_Seq unable to retrieve zone name."));
		EXM_LogFileWrite(_T("ERR"), EXM_LogMsg, EXM_ERR_CODE(1));
		return(PH_INVSOUR);
	}

	stat = GetObjtGenericTypeCode(ObjtIdx, &objtGenCode);
	if ( stat != RT_NORMAL ){
		_stprintf_s(EXM_LogMsg, _T("Error: Exec_Seq unable to retrieve object type generic code."));
		EXM_LogFileWrite(_T("ERR"), EXM_LogMsg, EXM_ERR_CODE(2));
		return (PH_INVOBJT);
	}
	
	stat = GetCellType(VCoo_X, VCoo_Y, &cellType);
	if ( stat != RT_NORMAL ){
		_stprintf_s(EXM_LogMsg, _T("Error: Exec_Seq unable to retrieve cell type."));
		EXM_LogFileWrite(_T("ERR"), EXM_LogMsg, EXM_ERR_CODE(2));
		return (PH_INVOBJT);
	}

	stat = GetSeqIdx(Code, objtGenCode, MAIN_MAST, ZoneIdx, cellType, &SeqIdx);
	if ( stat != RT_NORMAL ){
		_stprintf_s(EXM_LogMsg, _T("Error: Exec_Seq unable to retrieve micro op. sequence Idx."));
		EXM_LogFileWrite(_T("ERR"), EXM_LogMsg, EXM_ERR_CODE(2));
		return (PH_SEQNOTF);
	}

	/* GET SEQUENCE FROM 1090 FILE */
	file.num = F_OSEQ;
	retstat = FileRead( &file, (int)SeqIdx, 1, &seq , sizeof(t_ope_seq));
	if ( retstat != R_NORMAL ){
		_stprintf_s(EXM_LogMsg, _T("Error: Exec_Seq unable to read F.OSEQ DAFS file."));
		EXM_LogFileWrite(_T("ERR"), EXM_LogMsg, EXM_ERR_CODE(4));
		return (PH_SEQNOTF);
	}

	/* GET NEXT MOVE INDEX, ACTUAL MAST AND CURRENT PHASE FROM DYNAMIC PARAMETER */
	stat = GetDynNextMove_a ( &next_move_a );
	if(stat != RT_NORMAL){
		_stprintf_s(EXM_LogMsg, _T("Error: Exec_Seq unable to retrieve next automatic move."));
		EXM_LogFileWrite(_T("ERR"), EXM_LogMsg, EXM_ERR_CODE(5));
		return(PH_FAILED);
	}
	stat = GetDynCurrentPhase ( &Current_Phase );
	if(stat != RT_NORMAL){
		_stprintf_s(EXM_LogMsg, _T("Error: Exec_Seq unable to retrieve current phase."));
		EXM_LogFileWrite(_T("ERR"), EXM_LogMsg, EXM_ERR_CODE(5));
		return(PH_FAILED);
	}
	
	stat = GetMicroCmdIdx(MICRO_TV_INSP, &micrIdx);
	stat = GetMicroCmdIdx(MICRO_MOVE_FAGRL_MM, &FAGRLIdx);
	/* EXECUTE SEQUENCE */
	for ( i = 0; i < N_MAX_MIC_CMD; i++ ){
		if ( seq.micro_cmd[i].mic_name == 0 )
			break;
		memset ( &micro_exe, 0 , sizeof ( micro_exe ) );
		micro_exe.index = next_move_a;
		micro_exe.phase = Current_Phase;
		MicroCmdIdx = seq.micro_cmd[i].mic_name;
		if(MicroCmdIdx == micrIdx){
			markReq = 0;
			stat = GetPhasMarkReq(Current_Phase, &markReq);
			if((stat != RT_NORMAL) || (markReq == 0))	
				continue;
			
			else{
				//Metto la prossima operazione come indice della micro-op.//
				_tcsncpy_s(micro_exe.cmovet, MICRO_TV_INSP, L_MIC_CMD);
				micro_exe.cmovet[L_MIC_CMD] = '\0';
				micro_exe.movet = micrIdx;
				retstat = FileWrite(&file, next_move_a, 1, &micro_exe, sizeof(micro_exe));
				next_move_a++;
				stat = SetDynNextMove_a(next_move_a);		
			}
		}
		else{
			stat = GetMicroCmdName ( MicroCmdIdx, MicroCmdName );
			_tcsncpy_s ( micro_exe.cmovet, MicroCmdName, L_MIC_CMD );
			micro_exe.cmovet[L_MIC_CMD] = '\0';
			micro_exe.movet = MicroCmdIdx;
			micro_exe.Virt_X = (int)VCoo_X;
			micro_exe.Virt_Y = (int)VCoo_Y;
			if(MicroCmdIdx == FAGRLIdx)
				micro_exe.xyz_quo1 = (float)(seq.micro_cmd[i].code);
			else{
				if ( seq.micro_cmd[i].n_quotas >= 1 )
					micro_exe.xyz_quo1 = seq.micro_cmd[i].quota[0];
				if ( seq.micro_cmd[i].n_quotas >= 2 )
					micro_exe.z_quo_2 = seq.micro_cmd[i].quota[1];
				if ( seq.micro_cmd[i].n_quotas >= 3 )
					micro_exe.z_quo_3 = seq.micro_cmd[i].quota[2];
				if ( seq.micro_cmd[i].n_quotas >= 4 )
					micro_exe.z_quo_4 = seq.micro_cmd[i].quota[3];
			}
			file.num = F_MICE;
			retstat = FileWrite ( &file, next_move_a, 1, &micro_exe , sizeof(micro_exe));

			next_move_a++;
			stat = SetDynNextMove_a ( next_move_a );
		}
	}
	return (PH_NORMAL);
}

/********************************************************************************/
/*																				*/
/*	Function:		Exec_Seq_xy													*/
/*	Description:	Insert a micro-op in the F_MICE file. The micro op are 		*/
/*					MOVX, MOVY, MOVY_C, MOVXY.									*/
/*	Input:			MicroCmdName	-	Micro op to execute.					*/
/*					CoordX			-	Movement lenght on X axis.				*/
/*					CoordY			-	Movement lenght on Y axis.				*/
/*					Path_Type		-	Graphical option.						*/
/*	Return:			PH_NORMAL		-	Success									*/
/*																				*/
/********************************************************************************/
PHASTAT Exec_Seq_xy(MIC_CMDNAME MicroCmdName, float CoordX, float CoordY){
	RETSTAT			stat;
	RETCOND			retstat;
	float			startPX;
	float			startPY;
	int				Current_Phase;
	int				next_move_a;
	int				mastType;
	GENIDX			MicroCmdIdx;
	GENIDX			MovXIdx;
	GENIDX			MovYIdx;
	GENIDX			MovXYIdx;
	FILEIDX			file;
	micro_cmd_exe	micro_exe;
//	OBJTMM			ObjtMast;

	stat = GetDynCurrentMast(&mastType);
	if ( stat != RT_NORMAL )
		return(PH_FAILED);
	stat = GetMicroCmdIdx (MICRO_MOVEX, &MovXIdx );
	if ( stat != RT_NORMAL )
		return(PH_FAILED);
	stat = GetMicroCmdIdx ( MICRO_MOVEY, &MovYIdx );
	if ( stat != RT_NORMAL )
		return(PH_FAILED);
	stat = GetMicroCmdIdx ( MICRO_MOVEXY, &MovXYIdx );
	if ( stat != RT_NORMAL )
		return(PH_FAILED);
	
	stat = GetMicroCmdIdx ( MicroCmdName, &MicroCmdIdx );
	if ( stat != RT_NORMAL )
		return(PH_FAILED);

	stat = GetMastPosEx(mastType, &startPX, &startPY);
	if(stat != RT_NORMAL)
		return(PH_FAILED);

	stat = GetDynNextMove_a ( &next_move_a );
	if ( stat == RT_NORMAL ){
		if(MicroCmdIdx == MovXIdx){
			if((CoordX <= 0.0) || (CoordY != 0.0))
				return (PH_INVDEST);
			memset ( &micro_exe, 0 , sizeof ( micro_exe ) );
			stat = GetDynCurrentPhase ( &Current_Phase );
			//Metto la prossima operazione come indice della micro-op.//
			micro_exe.index = next_move_a;
			micro_exe.phase = Current_Phase;
			micro_exe.mast = mastType;
			micro_exe.startPX = startPX;
			micro_exe.startPY = startPY;
			stat = GetMicroCmdIdx ( MicroCmdName, &MicroCmdIdx );
			if ( stat == RT_NORMAL ){
				_tcsncpy_s ( micro_exe.cmovet, MicroCmdName, L_MIC_CMD );
				micro_exe.cmovet[L_MIC_CMD] = '\0';
				micro_exe.movet = MicroCmdIdx;
				//L'unico parametro per questo tipo di micro-op � la quota da raggiungere.//
				micro_exe.xyz_quo1 = CoordX;
				GetMastSituation(&micro_exe.objt_mm);
				//memcpy (&micro_exe.objt_mm, &ObjtMast, sizeof (OBJTMM));
				
				file.num = F_MICE;
				retstat = FileWrite ( &file, next_move_a, 1, &micro_exe, sizeof(micro_exe));
				next_move_a++;
				stat = SetDynNextMove_a ( next_move_a );
			}
		}
		else if(MicroCmdIdx == MovYIdx){
			if((CoordY <= 0.0) || (CoordX != 0.0))
				return (PH_INVDEST);
			memset ( &micro_exe, 0 , sizeof ( micro_exe ) );
			stat = GetDynCurrentPhase ( &Current_Phase );
			//Metto la prossima operazione come indice della micro-op.//
			micro_exe.index = next_move_a;
			micro_exe.phase = Current_Phase;
			micro_exe.mast = mastType;
			micro_exe.startPX = startPX;
			micro_exe.startPY = startPY;
			stat = GetMicroCmdIdx ( MicroCmdName, &MicroCmdIdx );
			if ( stat == RT_NORMAL ){
				_tcsncpy_s ( micro_exe.cmovet, MicroCmdName, L_MIC_CMD );
				micro_exe.cmovet[L_MIC_CMD] = '\0';
				micro_exe.movet = MicroCmdIdx;
				//L'unico parametro per questo tipo di micro-op � la quota da raggiungere.//
				micro_exe.xyz_quo1 = CoordY;
				GetMastSituation(&micro_exe.objt_mm);
				file.num = F_MICE;
				retstat = FileWrite ( &file, next_move_a, 1, &micro_exe, sizeof(micro_exe));
				next_move_a++;
				stat = SetDynNextMove_a ( next_move_a );
			}
		}
		
		else if(MicroCmdIdx == MovXYIdx){
			if((CoordX <= 0.0) || (CoordY < 0.0))
				return (PH_INVDEST);
			memset ( &micro_exe, 0 , sizeof ( micro_exe ) );
			stat = GetDynCurrentPhase ( &Current_Phase );
			//Metto la prossima operazione come indice della micro-op.//
			micro_exe.index = next_move_a;
			micro_exe.phase = Current_Phase;
			micro_exe.mast = mastType;
			micro_exe.startPX = startPX;
			micro_exe.startPY = startPY;
			stat = GetMicroCmdIdx ( MicroCmdName, &MicroCmdIdx );
			if ( stat == RT_NORMAL ){
				_tcsncpy_s ( micro_exe.cmovet, MicroCmdName, L_MIC_CMD );
				micro_exe.cmovet[L_MIC_CMD] = '\0';
				micro_exe.movet = MicroCmdIdx;
				//L'unico parametro per questo tipo di micro-op � la quota da raggiungere.//
				micro_exe.xyz_quo1 = CoordX;
				micro_exe.z_quo_2 = CoordY;
				GetMastSituation(&micro_exe.objt_mm);
				file.num = F_MICE;
				retstat = FileWrite ( &file, next_move_a, 1, &micro_exe, sizeof(micro_exe));
				next_move_a++;
				stat = SetDynNextMove_a ( next_move_a );
			}
		}
	}
	return (PH_NORMAL);
}

/********************************************************************************/
/*																				*/
/*	Function:		Exec_Seq_TV													*/
/*	Description:	Write on file F_MICE the micro-op TVROT.					*/
/*	Input:			angleRange	-	Rotation angle.								*/
/*	Return:			PH_NORMAL	-	Success										*/
/*																				*/
/********************************************************************************/
PHASTAT Exec_Seq_TV (float angleRange){  
	RETSTAT			stat;
	RETCOND			retstat;
	int				Current_Phase;
	int				next_move_a;
	float			StartPX;
	float			StartPY;
	GENIDX			MicroCmdIdx;
	TAGIDX			tagidx;
	FILEIDX			file;
	TAGVALEX		tagvalex;
	micro_cmd_exe	micro_exe;

	/* If there isn't TV don't put TV rotation sequence */
	
	retstat = GetTagIdx(TWN_TV_MOUNT, &tagidx);
	//retstat = RtdbReadValues ( 1, &tagidx, &tagval, &tagsta );
	retstat = TagRead(&tagidx, & tagvalex); 

	stat = GetMastPosEx(MAIN_MAST, &StartPX, &StartPY);
	if(stat != RT_NORMAL)
		return(PH_FAILED);

	if(GtBit((short)tagvalex.value.val_di, 15)){
		stat = GetDynNextMove_a ( &next_move_a );
		if ( stat == RT_NORMAL ){
			memset ( &micro_exe, 0 , sizeof ( micro_exe ) );
			stat = GetDynCurrentPhase ( &Current_Phase );
			micro_exe.index = next_move_a;
			micro_exe.phase = Current_Phase;
			micro_exe.startPX = StartPX;
			micro_exe.startPY = StartPY;
			stat = GetMicroCmdIdx ( MICRO_ROT_TV, &MicroCmdIdx );
			if ( stat == RT_NORMAL ){
				_tcsncpy_s ( micro_exe.cmovet, MICRO_ROT_TV, L_MIC_CMD );
				micro_exe.cmovet[L_MIC_CMD] = '\0';
				micro_exe.movet = MicroCmdIdx;
				micro_exe.xyz_quo1 = angleRange;
				//memcpy ( &micro_exe.objt_mm, ObjtMast, sizeof ( OBJTMM ) );
				file.num = F_MICE;
				retstat = FileWrite ( &file, next_move_a, 1, &micro_exe, sizeof(micro_exe));
				next_move_a++;
				stat = SetDynNextMove_a ( next_move_a );
				stat = SetDynTVAngle(angleRange);
			}
		}
	}
	return (PH_NORMAL);
}

/*********************************************************************************************
**********************************************************************************************
**
** Function:		EXM_ActivateLog
**
** Description:		Activate the Log file.
**
** Return:			int				- 0 for success.
**									- -1 otherwise.
** 
**********************************************************************************************
*********************************************************************************************/
int EXM_ActivateLog(void){
	EXM_LogActive = true;
	return 0;
}

/*********************************************************************************************
**********************************************************************************************
**
** Function:		EXM_DeactivateLog
**
** Description:		Deactivate the Log file.
**
** Return:			int				- 0 for success.
**									- -1 otherwise.
** 
**********************************************************************************************
*********************************************************************************************/
int EXM_DeactivateLog(void){
	EXM_LogActive = false;
	return 0;
}

/*********************************************************************************************
**********************************************************************************************
**
** Function:		PPH_ActivateMsgBox
**
** Description:		Activate the message box error messages.
**
** Return:			int				- 0 for success.
**									- -1 otherwise.
** 
**********************************************************************************************
*********************************************************************************************/
int EXM_ActivateMsgBox(void){
	EXM_MsgBoxActive = true;
	return 0;
}

/*********************************************************************************************
**********************************************************************************************
**
** Function:		PPH_DeactivateMsgBox
**
** Description:		Deactivate the message box error messages.
**
** Return:			int				- 0 for success.
**									- -1 otherwise.
** 
**********************************************************************************************
*********************************************************************************************/
int EXM_DeactivateMsgBox(void){
	EXM_MsgBoxActive = false;
	return 0;
}
/*********************************************************************************************
**********************************************************************************************
**
** Function:		EXM_SetLogPath
**
** Description:		Cahge the Log file path.
**
** Input:			path			- New Log file path.
** Return:			int				- 0 for success.
**									- -1 otherwise.
** 
**********************************************************************************************
*********************************************************************************************/
int EXM_SetLogPath(TCHAR *path){
	_tcscpy_s (EXM_LogPath, path);
	return 0;
}

/*********************************************************************************************
**********************************************************************************************
**
** Function:		EXM_LogFileWrite
**
** Description:		Print a message in the application log file,
**
** Input:			cmd				- _T("ERR") - error, _T("WRN") - warning, _T("OK ") - success.
**					Comment			- Message description.
**
** Return:			int				- 1 for success.
**									- -1 otherwise.
** 
**********************************************************************************************
*********************************************************************************************/
int EXM_LogFileWrite(TCHAR cmd[4], TCHAR *Comment, int errNum){
	FILE		*LogFile;
	TCHAR		newLine = '\n';
	SYSTEMTIME	timeStamp;
	TCHAR		tempStr[256];
	TCHAR		timeStr[100];
	
	if(EXM_LogActive){
		errno_t err;
		err = _tfopen_s(&LogFile,EXM_LogPath, _T("a"));
		if (err != 0)
			return -1;
		//LogFile = _tfopen(EXM_LogPath, _T("a"));
		//if(LogFile == NULL)
		//	return -1;
		
		GetSystemTime(&timeStamp);
		_stprintf_s(timeStr, _T("[hh%2d  mm%2d  ss%2d,%3d (GMT)] [%2d-%2d-%4d]"), timeStamp.wHour, timeStamp.wMinute, timeStamp.wSecond, timeStamp.wMilliseconds, timeStamp.wDay, timeStamp.wMonth, timeStamp.wYear);
		if(_tcscmp(cmd, _T("ERR")) == 0)
			_ftprintf(LogFile,_T("%s   %s  %s Err.N� %d.%c"), timeStr, cmd, Comment, errNum,  newLine);
		else
			_ftprintf(LogFile,_T("%s   %s  %s%c"), timeStr, cmd, Comment, newLine);
		if(fclose(LogFile)!=0)
			return -1;
	}
		
	if((EXM_MsgBoxActive) && (_tcscmp(cmd, _T("ERR")) == 0)){
		_stprintf_s(tempStr, _T("Error n.%d, %s"), errNum, Comment);
		MessageBox(NULL, tempStr, _T("Error"), MB_ICONWARNING||MB_OK);
	}

	return 0;
}

/*********************************************************************************************
**********************************************************************************************
**
** Function:		MiceOptimizer
**
** Description:		Optimize Mice file grouping operations of same types. 
**
** Input:			phase			- phase to optimize.
**
** Return:			RETSTAT			- RT_NORMAL for successfull optimization
**									- RT_FAILED otherwise.
** 
**********************************************************************************************
*********************************************************************************************/
RETSTAT MiceOptimizer(int phase){
	RETCOND			retCnd;
	RETSTAT			retSt;
	FILEIDX			fileMice;
	micro_cmd_exe	mice;
	micro_cmd_exe	temp;
	micro_cmd_exe	blank;
	int				rec, i, j;
	int				toPack;
	int				opNum;

	fileMice.num = F_MICE;
	opNum = 0;

	retSt = InitMice(&blank);

	for(rec = 1; rec <= N_MAX_MICENR; rec++){
		retCnd = FileRead(&fileMice, rec, 1, &mice, sizeof(mice));
		if(retCnd != R_NORMAL){
			return(RT_FAILED);
		}
		if(mice.phase == phase)
			opNum++;
		else if(mice.phase == 0)
			break;
	}

	for(rec = 1; rec <= N_MAX_MICENR; rec++){
		retCnd = FileRead(&fileMice, rec, 1, &mice, sizeof(mice));
		if(retCnd != R_NORMAL)
			return(RT_FAILED);
		if(mice.phase == 0)
			break;
		if(mice.phase == phase){
			if(!_tcscmp(mice.cmovet, MICRO_MOVEX)){
				toPack = 0;
				for(i = rec + 1; i <= N_MAX_MICENR; i++){
					retCnd = FileRead(&fileMice, i, 1, &temp, sizeof(mice));
					if(retCnd != R_NORMAL)
						return(RT_FAILED);
					if(!_tcscmp(temp.cmovet, MICRO_MOVEX))
						toPack++;
					else
						break;
				}
				if(toPack){
					retCnd = FileRead(&fileMice, rec + toPack, 1, &temp, sizeof(mice));
					if(retCnd != R_NORMAL)
						return(RT_FAILED);
					temp.index = mice.index;
					temp.startPX = mice.startPX;
					temp.startPY = mice.startPY;
					retCnd = FileWrite(&fileMice, rec, 1, &temp, sizeof(mice));
					if(retCnd != R_NORMAL)
						return(RT_FAILED);
					for(j = rec + 1; j <= rec + toPack; j++){
						retCnd = FileWrite(&fileMice, j, 1, &blank, sizeof(mice));
						if(retCnd != R_NORMAL)
							return(RT_FAILED);
					}
					for(j = rec + toPack + 1; j <= N_MAX_MICENR; j++){
						retCnd = FileRead(&fileMice, j, 1, &temp, sizeof(mice));
						if(retCnd != R_NORMAL)
							return(RT_FAILED);
						if(temp.phase == 0)
							break;
						temp.index = temp.index - toPack;
						retCnd = FileWrite(&fileMice, j - toPack, 1, &temp, sizeof(mice));
						if(retCnd != R_NORMAL)
							return(RT_FAILED);
					}
					for(j = opNum - toPack + 1; j <= opNum; j++){
						retCnd = FileWrite(&fileMice, j, 1, &blank, sizeof(mice));
						if(retCnd != R_NORMAL)
							return(RT_FAILED);
					}
					opNum = opNum - toPack;
				}
			}
			else if(!_tcscmp(mice.cmovet, MICRO_MOVEY)){
				toPack = 0;
				for(i = rec + 1; i <= N_MAX_MICENR; i++){
					retCnd = FileRead(&fileMice, i, 1, &temp, sizeof(mice));
					if(retCnd != R_NORMAL)
						return(RT_FAILED);
					if(!_tcscmp(temp.cmovet, MICRO_MOVEY))
						toPack++;
					else
						break;
				}
				if(toPack){
					retCnd = FileRead(&fileMice, rec + toPack, 1, &temp, sizeof(mice));
					if(retCnd != R_NORMAL)
						return(RT_FAILED);
					temp.index = mice.index;
					temp.startPX = mice.startPX;
					temp.startPY = mice.startPY;
					retCnd = FileWrite(&fileMice, rec, 1, &temp, sizeof(mice));
					if(retCnd != R_NORMAL)
						return(RT_FAILED);
					for(j = rec + 1; j <= rec + toPack; j++){
						retCnd = FileWrite(&fileMice, j, 1, &blank, sizeof(mice));
						if(retCnd != R_NORMAL)
							return(RT_FAILED);
					}
					for(j = rec + toPack + 1; j <= N_MAX_MICENR; j++){
						retCnd = FileRead(&fileMice, j, 1, &temp, sizeof(mice));
						if(retCnd != R_NORMAL)
							return(RT_FAILED);
						if(temp.phase == 0)
							break;
						temp.index = temp.index - toPack;
						retCnd = FileWrite(&fileMice, j - toPack, 1, &temp, sizeof(mice));
						if(retCnd != R_NORMAL)
							return(RT_FAILED);
					}
					for(j = opNum - toPack + 1; j <= opNum; j++){
						retCnd = FileWrite(&fileMice, j, 1, &blank, sizeof(mice));
						if(retCnd != R_NORMAL)
							return(RT_FAILED);
					}
					opNum = opNum - toPack;
				}
			}
			else if(!_tcscmp(mice.cmovet, MICRO_MOVE_FAGR_MM)){
				toPack = 0;
				for(i = rec + 1; i <= N_MAX_MICENR; i++){
					retCnd = FileRead(&fileMice, i, 1, &temp, sizeof(mice));
					if(retCnd != R_NORMAL)
						return(RT_FAILED);
					if(!_tcscmp(temp.cmovet, MICRO_MOVE_FAGR_MM))
						toPack++;
					else
						break;
				}
				if(toPack){
					retCnd = FileRead(&fileMice, rec + toPack, 1, &temp, sizeof(mice));
					if(retCnd != R_NORMAL)
						return(RT_FAILED);
					temp.index = mice.index;
					retCnd = FileWrite(&fileMice, rec, 1, &temp, sizeof(mice));
					if(retCnd != R_NORMAL)
						return(RT_FAILED);
					for(j = rec + 1; j <= rec + toPack; j++){
						retCnd = FileWrite(&fileMice, j, 1, &blank, sizeof(mice));
						if(retCnd != R_NORMAL)
							return(RT_FAILED);
					}
					for(j = rec + toPack + 1; j <= N_MAX_MICENR; j++){
						retCnd = FileRead(&fileMice, j, 1, &temp, sizeof(mice));
						if(retCnd != R_NORMAL)
							return(RT_FAILED);
						if(temp.phase == 0)
							break;
						temp.index = temp.index - toPack;
						retCnd = FileWrite(&fileMice, j - toPack, 1, &temp, sizeof(mice));
						if(retCnd != R_NORMAL)
							return(RT_FAILED);
					}
					for(j = opNum - toPack + 1; j <= opNum; j++){
						retCnd = FileWrite(&fileMice, j, 1, &blank, sizeof(mice));
						if(retCnd != R_NORMAL)
							return(RT_FAILED);
					}
					opNum = opNum - toPack;
				}
			}
		}
	}
	return(RT_NORMAL);
}

/*********************************************************************************************
**********************************************************************************************
**
** Function:		SetMiceTCInfo
**
** Description:		Set Mice file technological cycles data 
**
** Input:			startOp			- first operation
**					stopOp			- last operation (set stop flag also)
**					tecCycIdx		- Technological Cycle identifier
**					seqNum			- Number of op in TC Sequence
**
** Return:			RETSTAT			- RT_NORMAL for successfull data update
**									- RT_FAILED otherwise.
** 
**********************************************************************************************
*********************************************************************************************/
RETSTAT SetMiceTCInfo(int startOp, int stopOp, GENIDX tecCycIdx, int seqNum){
	RETCOND			retCnd;
	FILEIDX			fileMice;
	int				rec;
	micro_cmd_exe	mice;

	fileMice.num = F_MICE;

	if(startOp <= 0)
		return(RT_FAILED);
	if(stopOp > N_MAX_MICENR)
		return(RT_FAILED);

	for(rec = 1; rec <= N_MAX_MICENR; rec++){
		retCnd = FileRead(&fileMice, rec, 1, &mice, sizeof(mice));
		if(retCnd != R_NORMAL)
			return(RT_FAILED);
		if((mice.index >= startOp) && (mice.index <= stopOp)){
			mice.tecCycIdx = tecCycIdx;
			mice.tcIndex = seqNum;
			if(mice.index == stopOp)
				mice.stopFlag = true;
			retCnd = FileWrite(&fileMice, rec, 1, &mice, sizeof(mice));
			if(retCnd != R_NORMAL)
				return(RT_FAILED);
		}
		else if(mice.index > stopOp)
			break;
	}
	return(RT_NORMAL);
}

/*********************************************************************************************
**********************************************************************************************
**
** Function:		Exec_Seq_TV_Insp
**
** Description:		Register a TV Inspection in the next move to execute
**
** Return:			RETSTAT			- RT_NORMAL for successfull move save
**									- RT_FAILED otherwise.
** 
**********************************************************************************************
*********************************************************************************************/
PHASTAT Exec_Seq_TV_Insp(){
	RETCOND			retCnd;
	RETSTAT			retSt;
	FILEIDX			fileMice;
	micro_cmd_exe	mice;
	int				currPhas;
	int				nextMov;
	GENIDX			micrIdx;

	fileMice.num = F_MICE;

	retSt = GetDynCurrentPhase(&currPhas);
	if(retSt != RT_NORMAL)
		return(PH_FAILED);

	retSt = GetDynNextMove_a(&nextMov);
	if(retSt != RT_NORMAL)
		return(PH_FAILED);

	memset(&mice, 0 , sizeof (mice));
	//Metto la prossima operazione come indice della micro-op.//
	mice.index = nextMov;
	mice.phase = currPhas;
	retSt = GetMicroCmdIdx(MICRO_TV_INSP, &micrIdx);
	if(retSt == RT_NORMAL){
		_tcsncpy_s(mice.cmovet, MICRO_TV_INSP, L_MIC_CMD);
		mice.cmovet[L_MIC_CMD] = '\0';
		mice.movet = micrIdx;
		retCnd = FileWrite(&fileMice, nextMov, 1, &mice, sizeof(mice));
		nextMov++;
		retSt = SetDynNextMove_a(nextMov);
	}

	return(PH_NORMAL);
}

/*********************************************************************************************
**********************************************************************************************
**
** Function:		Exec_Seq_DCI
**
** Description:		Register a Fuel Assembly level monitor operation in the next move to execute
**
** Input:			svx				- Virtual Coordinate X of the cell to check
**					svy				- Virtual Coordinate Y of the cell to check
**
** Return:			RETSTAT			- RT_NORMAL for successfull move save
**									- RT_FAILED otherwise.
** 
**********************************************************************************************
*********************************************************************************************/
PHASTAT Exec_Seq_DCI(short int svx, short int svy){
	RETCOND			retCnd;
	RETSTAT			retSt;
	FILEIDX			fileMice;
	micro_cmd_exe	mice;
	int				currPhas;
	int				nextMov;
	GENIDX			micrIdx;
	GENIDX			mastType;
	
	fileMice.num = F_MICE;

	retSt = GetDynCurrentPhase(&currPhas);
	if(retSt != RT_NORMAL)
		return(PH_FAILED);

	retSt = GetDynNextMove_a(&nextMov);
	if(retSt != RT_NORMAL)
		return(PH_FAILED);

	retSt = GetDynCurrentMast(&mastType);
	if(retSt != RT_NORMAL)
		return(PH_FAILED);

	memset(&mice, 0 , sizeof (mice));
	//Metto la prossima operazione come indice della micro-op.//
	mice.index = nextMov;
	mice.phase = currPhas;
	mice.mast = mastType;
	mice.Virt_X = svx;
	mice.Virt_Y = svy;
	retSt = GetMicroCmdIdx(MICRO_CTRL_OP, &micrIdx);
	if(retSt == RT_NORMAL){
		_tcsncpy_s(mice.cmovet, MICRO_CTRL_OP, L_MIC_CMD);
		mice.cmovet[L_MIC_CMD] = '\0';
		mice.movet = micrIdx;
		mice.xyz_quo1 = 0;
		retCnd = FileWrite(&fileMice, nextMov, 1, &mice, sizeof(mice));
		nextMov++;
		retSt = SetDynNextMove_a(nextMov);
	}

	//Metto la prossima operazione come indice della micro-op.//
	mice.index = nextMov;
	mice.phase = currPhas;
	mice.mast = mastType;
	mice.Virt_X = svx;
	mice.Virt_Y = svy;
	retSt = GetMicroCmdIdx(MICRO_CTRL_OP, &micrIdx);
	if(retSt == RT_NORMAL){
		_tcsncpy_s(mice.cmovet, MICRO_CTRL_OP, L_MIC_CMD);
		mice.cmovet[L_MIC_CMD] = '\0';
		mice.movet = micrIdx;
		mice.xyz_quo1 = 1;
		retCnd = FileWrite(&fileMice, nextMov, 1, &mice, sizeof(mice));
		nextMov++;
		retSt = SetDynNextMove_a(nextMov);
	}

	return(PH_NORMAL);
}

/*********************************************************************************************
**********************************************************************************************
**
** Function:		Exec_Seq_DMP
**
** Description:		Register a FA mounting place inspection operation in the next move to execute
**
** Input:			svx				- Virtual Coordinate X of the cell to check
**					svy				- Virtual Coordinate Y of the cell to check
**
** Return:			RETSTAT			- RT_NORMAL for successfull move save
**									- RT_FAILED otherwise.
** 
**********************************************************************************************
*********************************************************************************************/
PHASTAT Exec_Seq_DMP(short int svx, short int svy){
	RETCOND			retCnd;
	RETSTAT			retSt;
	FILEIDX			fileMice;
	micro_cmd_exe	mice;
	int				currPhas;
	int				nextMov;
	GENIDX			micrIdx;
	
	fileMice.num = F_MICE;

	retSt = GetDynCurrentPhase(&currPhas);
	if(retSt != RT_NORMAL)
		return(PH_FAILED);

	retSt = GetDynNextMove_a(&nextMov);
	if(retSt != RT_NORMAL)
		return(PH_FAILED);

	memset(&mice, 0 , sizeof (mice));
	//Metto la prossima operazione come indice della micro-op.//
	mice.index = nextMov;
	mice.phase = currPhas;
	mice.mast = DMP_MAST;
	mice.Virt_X = svx;
	mice.Virt_Y = svy;
	retSt = GetMicroCmdIdx(MICRO_DMP_OP, &micrIdx);
	if(retSt == RT_NORMAL){
		_tcsncpy_s(mice.cmovet, MICRO_DMP_OP, L_MIC_CMD);
		mice.cmovet[L_MIC_CMD] = '\0';
		mice.movet = micrIdx;
		retCnd = FileWrite(&fileMice, nextMov, 1, &mice, sizeof(mice));
		nextMov++;
		retSt = SetDynNextMove_a(nextMov);
	}

	return(PH_NORMAL);
}

/*********************************************************************************************
**********************************************************************************************
**
** Function:		Exec_Seq_Displ
**
** Description:		Register a FA displacing operation in the next move to execute
**
** Input:			svx				- Virtual Coordinate X of the cell
**					svy				- Virtual Coordinate Y of the cell
**
** Return:			RETSTAT			- RT_NORMAL for successfull move save
**									- RT_FAILED otherwise.
** 
**********************************************************************************************
*********************************************************************************************/
PHASTAT Exec_Seq_Displ(short int svx, short int svy){
	RETCOND			retCnd;
	RETSTAT			retSt;
	FILEIDX			fileMice;
	micro_cmd_exe	mice;
	int				currPhas;
	int				nextMov;
	float			mmExpReact;
	float			tvExpReact;
	float			mmTransp;
	float			tvTransp;
	GENIDX			micrIdx;
	GENIDX			mastType;
	
	fileMice.num = F_MICE;

	retSt = GetDynCurrentPhase(&currPhas);
	if(retSt != RT_NORMAL)
		return(PH_FAILED);

	retSt = GetDynCurrentMast(&mastType);
	if(retSt != RT_NORMAL)
		return(PH_FAILED);

	retSt = GetDynNextMove_a(&nextMov);
	if(retSt != RT_NORMAL)
		return(PH_FAILED);

	retSt = GetInfiBlockValEx(8803, 0, TUNE_FROM_DAFS, &mmExpReact);
	if(retSt != RT_NORMAL)
		return(PH_FAILED);

	retSt = GetInfiBlockValEx(8528, 0, TUNE_FROM_DAFS, &tvExpReact);
	if(retSt != RT_NORMAL)
		return(PH_FAILED);

	retSt = GetInfiBlockValEx(8621, 0, TUNE_FROM_DAFS, &mmTransp);
	if(retSt != RT_NORMAL)
		return(PH_FAILED);

	retSt = GetInfiBlockValEx(8320, 0, TUNE_FROM_DAFS, &tvTransp);
	if(retSt != RT_NORMAL)
		return(PH_FAILED);

	memset(&mice, 0 , sizeof (mice));
	//Discesa del mast.//
	mice.index = nextMov;
	mice.phase = currPhas;
	mice.mast = MAIN_MAST;
	mice.Virt_X = svx;
	mice.Virt_Y = svy;
	retSt = GetMicroCmdIdx(MICRO_MOVE_FAGR_MM, &micrIdx);
	if(retSt == RT_NORMAL){
		_tcsncpy_s(mice.cmovet, MICRO_MOVE_FAGR_MM, L_MIC_CMD);
		mice.cmovet[L_MIC_CMD] = '\0';
		mice.movet = micrIdx;
		mice.xyz_quo1 = mmExpReact;
		mice.z_quo_2 = tvExpReact;
		retCnd = FileWrite(&fileMice, nextMov, 1, &mice, sizeof(mice));
		nextMov++;
		retSt = SetDynNextMove_a(nextMov);
	}

	retSt = GetDynNextMove_a(&nextMov);
	if(retSt != RT_NORMAL)
		return(PH_FAILED);

	memset(&mice, 0 , sizeof (mice));
	//Aggancio cassetta.//
	mice.index = nextMov;
	mice.phase = currPhas;
	mice.mast = MAIN_MAST;
	mice.Virt_X = svx;
	mice.Virt_Y = svy;
	retSt = GetMicroCmdIdx(MICRO_MOVE_FAGRL_MM, &micrIdx);
	if(retSt == RT_NORMAL){
		_tcsncpy_s(mice.cmovet, MICRO_MOVE_FAGRL_MM, L_MIC_CMD);
		mice.cmovet[L_MIC_CMD] = '\0';
		mice.movet = micrIdx;
		mice.xyz_quo1 = 0;
		retCnd = FileWrite(&fileMice, nextMov, 1, &mice, sizeof(mice));
		nextMov++;
		retSt = SetDynNextMove_a(nextMov);
	}

	retSt = GetDynNextMove_a(&nextMov);
	if(retSt != RT_NORMAL)
		return(PH_FAILED);

	memset(&mice, 0 , sizeof (mice));
	//Eseguo displ.//
	mice.index = nextMov;
	mice.phase = currPhas;
	mice.mast = MAIN_MAST;
	mice.Virt_X = svx;
	mice.Virt_Y = svy;
	retSt = GetMicroCmdIdx(MICRO_DISP_MM, &micrIdx);
	if(retSt == RT_NORMAL){
		_tcsncpy_s(mice.cmovet, MICRO_DISP_MM, L_MIC_CMD);
		mice.cmovet[L_MIC_CMD] = '\0';
		mice.movet = micrIdx;
		//1 indica raise req.//
		mice.xyz_quo1 = 1;
		retCnd = FileWrite(&fileMice, nextMov, 1, &mice, sizeof(mice));
		nextMov++;
		retSt = SetDynNextMove_a(nextMov);
	}

	retSt = GetDynNextMove_a(&nextMov);
	if(retSt != RT_NORMAL)
		return(PH_FAILED);

	memset(&mice, 0 , sizeof (mice));
	//Eseguo displ.//
	mice.index = nextMov;
	mice.phase = currPhas;
	mice.mast = MAIN_MAST;
	mice.Virt_X = svx;
	mice.Virt_Y = svy;
	retSt = GetMicroCmdIdx(MICRO_DISP_MM, &micrIdx);
	if(retSt == RT_NORMAL){
		_tcsncpy_s(mice.cmovet, MICRO_DISP_MM, L_MIC_CMD);
		mice.cmovet[L_MIC_CMD] = '\0';
		mice.movet = micrIdx;
		//0 indica low req.//
		mice.xyz_quo1 = 0;
		retCnd = FileWrite(&fileMice, nextMov, 1, &mice, sizeof(mice));
		nextMov++;
		retSt = SetDynNextMove_a(nextMov);
	}

	retSt = GetDynNextMove_a(&nextMov);
	if(retSt != RT_NORMAL)
		return(PH_FAILED);

	memset(&mice, 0 , sizeof (mice));
	//Eseguo apertura latch.//
	mice.index = nextMov;
	mice.phase = currPhas;
	mice.mast = MAIN_MAST;
	mice.Virt_X = svx;
	mice.Virt_Y = svy;
	retSt = GetMicroCmdIdx(MICRO_MOVE_FAGRL_MM, &micrIdx);
	if(retSt == RT_NORMAL){
		_tcsncpy_s(mice.cmovet, MICRO_MOVE_FAGRL_MM, L_MIC_CMD);
		mice.cmovet[L_MIC_CMD] = '\0';
		mice.movet = micrIdx;
		mice.xyz_quo1 = 1;
		retCnd = FileWrite(&fileMice, nextMov, 1, &mice, sizeof(mice));
		nextMov++;
		retSt = SetDynNextMove_a(nextMov);
	}

	retSt = GetDynNextMove_a(&nextMov);
	if(retSt != RT_NORMAL)
		return(PH_FAILED);

	memset(&mice, 0 , sizeof (mice));
	//Salita del mast.//
	mice.index = nextMov;
	mice.phase = currPhas;
	mice.mast = MAIN_MAST;
	mice.Virt_X = svx;
	mice.Virt_Y = svy;
	retSt = GetMicroCmdIdx(MICRO_MOVE_FAGR_MM, &micrIdx);
	if(retSt == RT_NORMAL){
		_tcsncpy_s(mice.cmovet, MICRO_MOVE_FAGR_MM, L_MIC_CMD);
		mice.cmovet[L_MIC_CMD] = '\0';
		mice.movet = micrIdx;
		mice.xyz_quo1 = mmTransp;
		mice.z_quo_2 = tvTransp;
		retCnd = FileWrite(&fileMice, nextMov, 1, &mice, sizeof(mice));
		nextMov++;
		retSt = SetDynNextMove_a(nextMov);
	}

	return(PH_NORMAL);
}

